package com.changgou.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;


    @Override
    public void add(Integer num, Long id,String username) {
        if (num <= 0) {
            redisTemplate.boundHashOps("Cart_" + username).delete(id.toString());
            Long size = redisTemplate.boundHashOps("Cart_" + username).size();

            if (size == null || size <= 0) {
                redisTemplate.delete("Cart_" + username);
            }

            return;

        }

        Result<Sku> skuResult = skuFeign.findById(id.toString());
        Sku sku = skuResult.getData();
        Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());

        Spu spu = spuResult.getData();

        //将SKU转换成OrderItem
        OrderItem orderItem = sku2OrderItem(sku,spu, num);

        /******
         * 购物车数据存入到Redis
         * namespace = Cart_[username]
         * key=id(sku)
         * value=OrderItem
         */
        redisTemplate.boundHashOps("Cart_"+username).put(id+"", orderItem);

    }


    /***
     * 查询用户购物车数据
     * @param username
     * @return
     */
    @Override
    public List list(String username) {
        //查询所有购物车数据
        List orderItems = redisTemplate.boundHashOps("Cart_" + username).values();
        return orderItems;
    }

    /***
     * SKU转成OrderItem
     * @param sku
     * @param num
     * @return
     */
    private OrderItem sku2OrderItem(Sku sku,Spu spu,Integer num){
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num*orderItem.getPrice());       //单价*数量
        orderItem.setPayMoney(num*orderItem.getPrice());    //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight()*num);           //重量=单个重量*数量

        //分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
